package cn.com.swu.mianyangapp.ui.news;

import android.net.wifi.aware.DiscoverySession;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.android.material.tabs.TabLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.com.swu.mianyangapp.R;
import cn.com.swu.mianyangapp.ui.bean.News;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NewsHomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsHomeFragment extends Fragment {

    private final String user = "likang";
    private final int userid = 1;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<String> mTitleList;
    private List<Fragment> mFragmentList;

    private NewsControllerAdapter mNewsControllerAdapter;

    public NewsHomeFragment() {
        // Required empty public constructor
    }


    public static NewsHomeFragment newInstance(String param1, String param2) {
        NewsHomeFragment fragment = new NewsHomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_news_home, container, false);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initData();
        mViewPager = view.findViewById(R.id.news_home_vp);
        mTabLayout = view.findViewById(R.id.news_home_layout);
        mNewsControllerAdapter = new NewsControllerAdapter(getChildFragmentManager(),mFragmentList,mTitleList);
        mViewPager.setAdapter(mNewsControllerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initData() {
        mFragmentList = new ArrayList<>();
        HomeNewsFragment fragment1 = HomeNewsFragment.newInstance("我的新闻", "");
        HomeNewsFragment fragment2 = HomeNewsFragment.newInstance("我的草稿", "");

        mFragmentList.add(fragment1);
        mFragmentList.add(fragment2);


        mTitleList = new ArrayList<>();

        mTitleList.add("我的新闻");
        mTitleList.add("我的草稿");


    }
}